<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>   
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title><s:if test="item.pkId==null||item.pkId==0">添加</s:if><s:else>修改</s:else>部门信息</title>
		<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
	</head>
	<body>
		<div class="navBar">
			当前位置：系统管理&nbsp;&gt;&gt;&nbsp;组织机构管理 &nbsp;&gt;&gt;&nbsp;<s:if test="item.pkId==null||item.pkId==0">添加</s:if><s:else>修改</s:else>部门
		</div>
		<div class="tabBlock">
			<div class="editBlock">
				<s:form action="deptInfo-save.action" theme="simple">
					<table>
						<tbody>
							<tr>
								<th colspan="2"><s:if test="item.pkId==null||item.pkId==0">添加</s:if><s:else>修改</s:else>部门</th>
							</tr>
							<tr>
								<th style="color:#000000;">
									上级机构：
								</th>
								<td>
									<s:hidden id="pkId" name="item.pkId"></s:hidden>
									<s:hidden id="orgId" name="item.orgId"></s:hidden>
									<s:hidden id="rootDeptId" name="item.org.rootDept.pkId"></s:hidden>
									<s:hidden id="relevanceConfig" name="item.relevanceConfig"></s:hidden>
									<s:select id="parentId" name="item.parId" list="page.condition.parentNavList" listKey="pkId" listValue="name" onchange="parentChanage(this)"></s:select>
								</td>
							</tr>
							<tr>
								<th style="color:#000000;">
									名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：
								</th>
								<td>
									<s:textfield id="name" name="item.name" maxlength="50" cssClass="inputText" onchange="nameChanged()"></s:textfield>
								</td>
							</tr>
							<tr class="orgName" style="display: none;">
								<th style="color:#000000;">
									全&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：
								</th>
								<td>
									<s:textfield id="fullName" name="item.org.name" maxlength="50" cssClass="inputText"></s:textfield>
								</td>
							</tr>
							<tr>
								<th style="color:#000000;">
									编&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：
								</th>
								<td>
									<s:textfield id="deptNo" name="item.deptNo" maxlength="20" cssClass="inputText"></s:textfield>
								</td>
							</tr>
							<tr>
								<th style="color:#000000;">
									类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型：
								</th>
								<td>
									<s:radio id="deptType" name="item.deptType" list="page.condition.deptTypes" listKey="value" listValue="displayName" onchange="deptTypeChanged(this.value)"></s:radio>
								</td>
							</tr>
							<tr style="<s:if test="orgTypeList==null||orgTypeList.size==0">display: none;</s:if>">
								<th style="color:#000000;">
									组织架构：
								</th>
								<td>
									<s:select id="orgTypeId" name="item.org.orgType.pkId" list="orgTypeList" listKey="pkId" listValue="name" emptyOption="true" onchange="orgTypeChanged(this)" disabled="item.deptType!=2"/>
								</td>
							</tr>
							<tr class="orgDeptLevel" style="<s:if test="orgTypeList==null||orgTypeList.size==0||item.org.orgType==null">display: none;</s:if>">
								<th style="color:#000000;">
									级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别：
								</th>
								<td>
									<s:select id="orgDeptLevel" name="orgDeptLevel" list="orgDeptLevelList" listKey="pkId" listValue="name" emptyOption="true" />
								</td>
							</tr>
							<tr class="org">
								<th style="color:#000000;">
									关联类型：
								</th>
								<td>
									<s:select id="relevanceType" name="relevanceType" list="#{'':'','RTX':'腾讯通组织架构','DingTalk':'钉钉组织架构'}" onchange="relevanceTypeChanged(this)"/>
								</td>
							</tr>
							<tr class="relevanceConfig rtx" style="display: none;">
								<th style="color:#000000;">
									接口地址：
								</th>
								<td>
									<s:textfield id="address" name="address" maxlength="40" cssClass="inputText" onchange="validateRtxConf()"></s:textfield>
								</td>
							</tr>
							<tr class="relevanceConfig dingding" style="display: none;">
								<th style="color:#000000;">
									公司ID：
								</th>
								<td>
									<s:textfield id="CorpID" name="CorpID" maxlength="40" cssClass="inputText" onchange="validateDingConf()"></s:textfield>
								</td>
							</tr>
							<tr class="relevanceConfig dingding" style="display: none;">
								<th style="color:#000000;">
									通信密钥：
								</th>
								<td>
									<s:textfield id="CorpSecret" name="CorpSecret" maxlength="70" cssClass="inputText" onchange="validateDingConf()"></s:textfield>
								</td>
							</tr>
							<tr class="relevanceConfig dingding rtx" style="display: none;">
								<th style="color:#000000;">
									关联部门：
								</th>
								<td>
									<s:select id="RelevanceDepartment" name="RelevanceDepartment" list="#{}" emptyOption="true" onchange="relevanceDeptChanged()"></s:select>
								</td>
							</tr>
							<tr>
								<th colspan="4"
									style="color:red;text-align:left;padding-left:10px;"><s:actionmessage />
									<s:actionerror /></th>
							</tr>
						</tbody>
					</table>
					<div style="text-align: center;">
						<a class="but28" onclick="save();" style="text-decoration:none;margin: 5px 10px;" >保存</a>
						<a class="but28" onclick="cancel();" style="text-decoration:none;margin: 5px 10px;" >取消</a>
					</div>
				</s:form>
			</div>
		</div>
		<script src="<%=basePath %>js/jquery.js" type="text/javascript"></script>
		<script type="text/javascript">
			window.returnValue = "";
			var merge=false;
			var duplication = false;
			// 初始关联类型，作为是否改变的依据
			var initRelevanceType;
			
			$(document).ready(function(){

				// 部门类型处理
				var deptType = $("input[name='item.deptType']:checked").val();
				if (deptType!="2"){
					$("#fullName").val("");
				}
				deptTypeChanged(deptType);
				
				// 关联配置处理
				var relevanceConfig = $("#relevanceConfig").val();
				if (relevanceConfig==""){
					relevanceConfig = "{}";
				}
				relevanceConfig = eval("("+relevanceConfig+")");
				// 初始化初始关联类型
				if (relevanceConfig.relevanceType){
					initRelevanceType = relevanceConfig.relevanceType;
				}else{
					initRelevanceType = "";
				}
				$("#relevanceType").val(initRelevanceType);
				relevanceConfigInit(relevanceConfig);
				relevanceTypeChanged($("#relevanceType")[0]);

			});

			function relevanceConfigInit(relevanceConfig){
				if (relevanceConfig.relevanceType=="RTX"){
					if (relevanceConfig.address){
						$("#address").val(relevanceConfig.address);
					}
					if (relevanceConfig.RelevanceDepartment){
						$("#RelevanceDepartment")[0].options.add(new Option("["+relevanceConfig.RelevanceDepartment+"]",relevanceConfig.RelevanceDepartment,true,true));
					}else{
						
					}
				} else if (relevanceConfig.relevanceType=="DingTalk"){
					if (relevanceConfig.CorpID){
						$("#CorpID").val(relevanceConfig.CorpID);
					}
					if (relevanceConfig.CorpSecret){
						$("#CorpSecret").val(relevanceConfig.CorpSecret);
					}
					if (relevanceConfig.RelevanceDepartment){
						$("#RelevanceDepartment")[0].options.add(new Option(relevanceConfig.RelevanceDepartment, relevanceConfig.RelevanceDepartment, true, true))
					}
					validateDingConf();
				}
			}
			
			function validateRelevance(){
				var relevanceType = $("#relevanceType").val();
				var relevanceConfig = $("#relevanceConfig").val();
				if (relevanceConfig==""){
					relevanceConfig = "{}";
				}
				relevanceConfig = eval("("+relevanceConfig+")");
				if (!relevanceConfig.relevanceType){
					relevanceConfig.relevanceType = "";
				}
				if (relevanceType==""){
					$("#relevanceConfig").val("");
				}else{
					if (relevanceType=="RTX"){
						if (relevanceConfig.RelevanceDepartment){
							$("#RelevanceDepartment")[0].options.add(new Option(relevanceConfig.RelevanceDepartment, relevanceConfig.RelevanceDepartment, true, true))
						}
						validateRtxConf();
					} else if (relevanceType=="DingTalk"){
						if (relevanceConfig.CorpID){
							$("#CorpID").val(relevanceConfig.CorpID);
						}
						if (relevanceConfig.CorpSecret){
							$("#CorpSecret").val(relevanceConfig.CorpSecret);
						}
						if (relevanceConfig.RelevanceDepartment){
							$("#RelevanceDepartment")[0].options.add(new Option(relevanceConfig.RelevanceDepartment, relevanceConfig.RelevanceDepartment, true, true))
						}
						validateDingConf();
					}
				}
			}
			
			function orgTypeName(){
				return $('#typeName').html();
			}
			
			function cancel(){
    			window.close();
			}
			
			function parentChanage(_this){
				var pkId = $("#pkId").val();
				var parentId = $("#parentId").val();
				$.ajax({
					url: "Dept-getParentSelectList.action",
					type: "POST",
					data: {"item.pkId":pkId,"item.parent.pkId":_this.value},
					dataType: "json",
					success: function(result){
						if (result.success){
							
							_this.options.length=0;
							for(var i=0;i<result.parentSelectList.length;i++){
								var option = new Option(result.parentSelectList[i].name,result.parentSelectList[i].pkId);
								if (option.value==parentId){
									option.selected = true;
								}
								_this.add(option); 
							}
							_this.value
						}else{
							alert("访问失败，请稍后再试："+result.errorMessage);
						}
					}
				});
				validateUni();
			}
			
			function nameChanged(){
				validateUni();
			}
			
			function deptTypeChanged(deptType){
				if (deptType=="2"){
					$("TR.orgName").show();
					if ($("#fullName").val()==""){
						$("#fullName").val($("#name").val());
					}
				}else{
					var deptId = $("#pkId").val();
					if(deptId==''){
						
					}else{
						// 不再是组织机构了，查询上一级组织机构
						$.ajax({
							url: "deptInfo-loadParentOrg.action",
							type: "POST",
							data: {"item.pkId":deptId},
							dataType: "json",
							success: function(result){
								if(result.success){
									$("#fullName").val(result.orgName);
									$("TR.orgName").hide();
								}else{
									alert("获取上级组织机构信息失败："+result.errorMessage);
								}
							}
						});
					}
				}
			}
			
			function relevanceTypeChanged(_this){
				validateRelevance();
				if (_this.value==""){
					$("TR.dingding").hide();
					$("TR.rtx").hide();
				}else if(_this.value=="RTX"){
					$("TR.dingding").hide();
					$("TR.rtx").show();
				}else if(_this.value=="DingTalk"){
					$("TR.dingding").show();
				}else{
					$("TR.dingding").hide();
				}
			}
			
			function orgTypeChanged(_this){
				$("#orgDeptLevel").empty();
				if (_this.value==""){
					$("TR.orgDeptLevel").hide();
				}else{
					$.ajax({
						url: "deptInfo-loadOrgDeptLevelList.action",
						type: "POST",
						data: {"item.org.orgType.pkId":_this.value},
						dataType: "json",
						success: function(result){
							if(result.success){
								$("#orgDeptLevel").append("<option value=''></option>");
								for(var i=0;i<result.orgDeptLevelList.length;i++){
									var item = result.orgDeptLevelList[i];
									$("#orgDeptLevel").append("<option value='"+item.pkId+"'>"+item.name+"</option>");
								}
							}else{
								alert("获取组织架构类型列表失败："+result.errorMessage);
							}
						}
					});
					$("TR.orgDeptLevel").show();
				}
			}
			
			function relevanceDeptChanged(){
				var relevanceType = $("#relevanceType").val();
				if (relevanceType=="RTX"){
					var address = $.trim($("#address").val());
					var RelevanceDepartment = $.trim($("#RelevanceDepartment").val());
					$("#relevanceConfig").val('{"relevanceType":"'+relevanceType+'","address":"'+address+'","RelevanceDepartment":"'+RelevanceDepartment+'"}');
				}else if(relevanceType=="DingTalk"){
					var CorpID = $.trim($("#CorpID").val());
					var CorpSecret = $.trim($("#CorpSecret").val());
					var RelevanceDepartment = $.trim($("#RelevanceDepartment").val());
					$("#relevanceConfig").val('{"relevanceType":"'+relevanceType+'","CorpID":"'+CorpID+'","CorpSecret":"'+CorpSecret+'","RelevanceDepartment":"'+RelevanceDepartment+'"}');
				}
			}
			
			function validateUni(){
				// 验证唯一性
				$.ajax({
					url: "Dept-validateUni.action",
					type: "POST",
					data: {
						"item.pkId": $("#pkId").val(),
						"item.parId":$("#parentId").val(),
						"item.name":$("#name").val()
					},
					dataType: "json",
					success: function(result){
						if (result.success){
							if (result.result){
								duplication = false;
							}else{
								merge = confirm("有同名的部门，是要合并吗？");
								duplication = !merge;
							}
						}else{
							alert("唯一性验证失败，请稍后再试："+result.errorMessage);
						}
					}
				});
			}
			
			function validateRtxConf(){
				var address = $.trim($("#address").val());
				var RelevanceDepartment = $.trim($("#RelevanceDepartment").val());
				var relevanceConfig = '{"relevanceType":"RTX","address":"'+address+'","RelevanceDepartment":"'+RelevanceDepartment+'"}';
				$("#relevanceConfig").val(relevanceConfig);
				loadRelevanceDeptTreeList(relevanceConfig);
			}
			
			function validateDingConf(){
				var CorpID = $.trim($("#CorpID").val());
				var CorpSecret = $.trim($("#CorpSecret").val());
				var RelevanceDepartment = $.trim($("#RelevanceDepartment").val());
				var relevanceConfig = "{'relevanceType':'DingTalk','CorpID':'"+CorpID+"','CorpSecret':'"+CorpSecret+"','RelevanceDepartment':'"+RelevanceDepartment+"'}";
				$("#relevanceConfig").val(relevanceConfig);
				if (CorpID.length>=20&&CorpSecret.length>=64){
					loadRelevanceDeptTreeList(relevanceConfig);
				}
			}
			
			function loadRelevanceDeptTreeList(relevanceConfig){
				var pkId = $("#pkId").val();
				$.ajax({
					url: "deptInfo-getRelevanceDeptTreeList.action",
					type: "POST",
					data: {"item.pkId":pkId,"item.relevanceConfig": relevanceConfig},
					dataType: "json",
					success: function(result){
						if (result.success){
							var curValue = $("#RelevanceDepartment").val();
							var _this = $("#RelevanceDepartment")[0];
							
							_this.options.length=0;
							_this.add(new Option("","")); 
							for(var i=0;i<result.result.length;i++){
								var item = result.result[i];
								var option = new Option(item.name,item.name);
								_this.add(option); 
							}
							_this.value=curValue;
						}else{
							alert("访问失败，请稍后再试："+result.errorMessage);
						}
					}
				});
			}
			
			function getRootDeptId(deptType){
				var rootDeptId;
				if (deptType=="2"){
					rootDeptId = $("#pkId").val();
				}else{
					rootDeptId = "";
				}
				return rootDeptId;
			}
			
			function save() {
				var pkId = $("#pkId").val();
				var parentId = $("#parentId").val();
				var relevanceType = $("#relevanceType").val();
				var orgTypeId = $("#orgTypeId").val();
				var relevanceConfig;
				if(relevanceType!=""){
					relevanceConfig = $("#relevanceConfig").val();
				}else{
					relevanceConfig = "";
				}
				var name = $.trim($("#name").val());
				var deptType = $("input[name='item.deptType']:checked").val();
				var rootDeptId = getRootDeptId(deptType);
				if(name==''){
					alert("名称不能为空！");
					return;
				}
				var deptNo = $.trim($("#deptNo").val());
				if(deptNo.length>20){
					alert(orgTypeName()+"编号长度不能超过20个字符！");
					return;
				}
				var data;
				if (deptType=="2"){
					var fullName = $("#fullName").val();
					data = {"item.pkId":pkId,"item.parId":parentId,"item.name":name,"item.deptNo":deptNo,"item.deptType":deptType,"item.org.rootDept.pkId":rootDeptId,"item.org.orgType.pkId":orgTypeId,"item.relevanceConfig":relevanceConfig,"item.org.name":fullName};
				}else{
					data = {"item.pkId":pkId,"item.parId":parentId,"item.name":name,"item.deptNo":deptNo,"item.deptType":deptType,"item.org.rootDept.pkId":rootDeptId,"item.relevanceConfig":relevanceConfig};
				}
				var action = $("form")[0].action;
				$.ajax({
					url:action,
					type: "post", 
					data: data,
					dataType:"json",
					success: function(result){
						if (result.success){
							if (relevanceType!=initRelevanceType){
								window.returnValue = "{success:true,resultType:2,message:'结构有改变，需要刷新组织机构树！'}";
							}else{
								window.returnValue = "{success:true,resultType:1,message:'内容有改变，需要刷新组织机构列表！'}";
							}
			    			window.close();
						}else{
							alert("保存失败："+result.errorMessage);
						}
					}
				});
			}
			
		</script>
	</body>
</html>